package com.asiainfo.dacp.datastash.broker.core;

import com.asiainfo.dacp.core.Configuration;
import com.asiainfo.dacp.datastash.broker.core.vo.StashUnitDefine;
import com.asiainfo.dacp.datastash.broker.util.HostIpHelper;

import com.asiainfo.dacp.metamodel.domain.ds.model.MetaDataSource;
import lombok.Getter;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 *
 * @author MeiKefu
 * @date 2017-10-31
 */
public class BrokerContext {
	
    public static Logger LOG = LoggerFactory.getLogger(BrokerContext.class);  

    public static String ZK_ADDRESS;
    
    public static final String HB_ADDRESS="/datastash/broker/heartbeat";
    public static final String ACTIVE_BROKERS="/datastash/broker/active"; 
    
    
    public static final String ENV_PROPERTIES="datastash-env-config.properties";

    @Getter
    private String brokerId;

    @Getter
    private String brokerIp;

    @Getter
    private Map<String,StashUnitDefine> stashUnitDefs = new LinkedHashMap<>();

    @Getter
    private Map<String,MetaDataSource> dsDefs = new HashMap<>();

    private static BrokerContext instance = new BrokerContext();

    private BrokerContext(){
        brokerId = "broker1";
        Properties props = new Properties();
        InputStream in = Configuration.class.getClassLoader()
				.getResourceAsStream("conf/" + ENV_PROPERTIES);
        try {
			props.load(in);
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error("配置文件错误");
		}
        brokerId=  props.getProperty("broker.id");	
        brokerIp = HostIpHelper.getHostIp();
        ZK_ADDRESS = props.getProperty("zk.addr");
    }

    public static BrokerContext getInstance(){
        return instance;
    }

    public StashUnitDefine getStashUnitDefine(String unitCode){
        return stashUnitDefs.get(unitCode);
    }
}
